SettingPanel = class("SettingPanel")

function SettingPanel.create(parent)
    local panel = SettingPanel.new()
    panel:init(parent)

    panel.view:setPosition(pp.center.x, pp.center.y)
    uiEffect.bounceIn(panel.view)    
    parent:base_onOpenWin(panel)
    return panel
end

function SettingPanel:init(parent)
    self.view = ccs.GUIReader:getInstance():widgetFromJsonFile("settingPanel.json")
    parent.widget:addChild(self.view)
    self.view:setLocalZOrder(pp.z_pause)
    self.parent = parent

    local function btCloseCallback(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            AudioEngine.playEffect(sound.btn2)
        elseif eventType == ccui.TouchEventType.ended then
            -- uiEffect.maskOut(self.parent.mask)
            uiEffect.bounceOut(self.view, true)
            -- self.parent.settingPanel = nil
            self.parent:base_onCloseWin(self)
            
        end
    end
    self.bt_close = self.view:getChildByName("_close")
    self.bt_close:addTouchEventListener(btCloseCallback)


    local function btMusicOnCallback(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            AudioEngine.playEffect(sound.btn1)
         elseif eventType == ccui.TouchEventType.ended then
             gameUtil.musicOn()
             self:refreshBtnsStatus()
             
         end
    end
    self.bt_musicOn = self.view:getChildByName("_open_music")
    self.bt_musicOn:addTouchEventListener(btMusicOnCallback)
    
    local function btMusicOffCallback(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            AudioEngine.playEffect(sound.btn1)
        elseif eventType == ccui.TouchEventType.ended then
            gameUtil.musicOff()
            self:refreshBtnsStatus()
            
        end
    end
    self.bt_musicOff = self.view:getChildByName("_close_music")
    self.bt_musicOff:addTouchEventListener(btMusicOffCallback)


    local function btsoundOnCallback(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            AudioEngine.playEffect(sound.btn1)
         elseif eventType == ccui.TouchEventType.ended then
             gameUtil.soundOn()
             self:refreshBtnsStatus()
             
         end
    end
    self.bt_soundOn = self.view:getChildByName("_open_sound")
    self.bt_soundOn:addTouchEventListener(btsoundOnCallback)
    
    local function btsoundOffCallback(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            AudioEngine.playEffect(sound.btn1)
        elseif eventType == ccui.TouchEventType.ended then
            gameUtil.soundOff()
            self:refreshBtnsStatus()
        end
    end
    self.bt_soundOff = self.view:getChildByName("_close_sound")
    self.bt_soundOff:addTouchEventListener(btsoundOffCallback)

    self:refreshBtnsStatus()
end 

function SettingPanel:refreshBtnsStatus( )
	-- body
	if userData.effectOn then
		self.bt_soundOn:setEnabled(false)
        self.bt_soundOn:loadTextureNormal("common/k-a.png", ccui.TextureResType.localType)
        
		self.bt_soundOff:setEnabled(true)
        self.bt_soundOff:loadTextureNormal("common/g-l.png", ccui.TextureResType.localType)        
	else
		self.bt_soundOn:setEnabled(true)
		self.bt_soundOn:loadTextureNormal("common/k-l.png", ccui.TextureResType.localType)
		self.bt_soundOff:setEnabled(false)
		self.bt_soundOff:loadTextureNormal("common/g-a.png", ccui.TextureResType.localType)
	end

	if userData.musicOn then
		self.bt_musicOn:setEnabled(false)
		self.bt_musicOn:loadTextureNormal("common/k-a.png", ccui.TextureResType.localType)
		self.bt_musicOff:setEnabled(true)
		self.bt_musicOff:loadTextureNormal("common/g-l.png", ccui.TextureResType.localType)
	else
		self.bt_musicOn:setEnabled(true)
		self.bt_musicOn:loadTextureNormal("common/k-l.png", ccui.TextureResType.localType)
		self.bt_musicOff:setEnabled(false)
		self.bt_musicOff:loadTextureNormal("common/g-a.png", ccui.TextureResType.localType)
	end
end
